package mongoDB

import com.mongodb.MongoClient
import com.mongodb.DB
import java.net.UnknownHostException
import com.mongodb.DBCollection
import net.vz.mongodb.jackson.JacksonDBCollection

object SystemDB {
	var mongoClient: MongoClient = null
	var db: DB = null
	
	def create {
		try {
			mongoClient = new MongoClient("localhost", 27017)
		} catch {
		  case e: UnknownHostException => {
		    throw new RuntimeException(e)
		  }
		}
			
		db = mongoClient.getDB("persistencia")
	}
	
	def collection[T <: {def id: String}](entityType: Class[T]): Collection[T] = {
		var dbCollection: DBCollection = db.getCollection(entityType.getSimpleName())
		return new Collection[T](JacksonDBCollection.wrap(dbCollection, entityType, classOf[String]))
	}
  
}